
package com.wyzz.hr.action;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.axis.utils.StringUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IRecruitapplierBo;
import com.wyzz.hr.pojo.Recruitapplier;
import com.wyzz.hr.pojo.Recruitplan;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class RecruitapplierCreate extends BaseAction {

	public RecruitapplierCreate() {
		page = new Pager();
		CHINESE_RESUME = "\u4E2D\u6587";
		ENGLISH_RESUME = "\u5916\u6587";
	}

	public String execute() throws Exception {
		IRecruitapplierBo applierBo = (IRecruitapplierBo) getBean("applierBo");
		if (resume1 != null)
			upload(resume1, CHINESE_RESUME);
		if (resume2 != null)
			upload(resume2, ENGLISH_RESUME);
		if (applier.getRecaPlanId() != null
				&& StringUtils.isEmpty(applier.getRecaPlanId().getId())
				|| "null".equalsIgnoreCase(applier.getRecaPlanId().getId()))
			applier.setRecaPlanId(null);
		if (applier.getRecaReferEmpNo() != null
				&& StringUtils.isEmpty(applier.getRecaReferEmpNo().getId())
				|| "null".equalsIgnoreCase(applier.getRecaReferEmpNo().getId()))
			applier.setRecaReferEmpNo(null);
		if (applier.getRecaInterviewer() != null
				&& StringUtils.isEmpty(applier.getRecaInterviewer().getId())
				|| "null"
						.equalsIgnoreCase(applier.getRecaInterviewer().getId()))
			applier.setRecaInterviewer(null);
		if (applier.getRecaRelationEmpNo() != null
				&& StringUtils.isEmpty(applier.getRecaRelationEmpNo().getId())
				|| "null".equalsIgnoreCase(applier.getRecaRelationEmpNo()
						.getId()))
			applier.setRecaRelationEmpNo(null);
		boolean isNewAdd = true;
		if (StringUtils.isEmpty(applier.getId()))
			applier.setId(null);
		else
			isNewAdd = false;
		if (isNewAdd)
			errors = applierBo.insertApplier(applier, getCurrentEmpNo());
		else
			errors = applierBo.updateApplier(applier, getCurrentEmpNo());
		if (errors.size() >= 1) {
			String error = (String) errors.get(0);
			addErrorInfo(error);
			return "input";
		}
		if (isNewAdd)
			addSuccessInfo((new StringBuilder()).append("\u6DFB\u52A0").append(
					applier.getRecaName()).append(
					"\u7684\u7B80\u5386\u6210\u529F\u3002").toString());
		else
			addSuccessInfo((new StringBuilder()).append("\u4FEE\u6539").append(
					applier.getRecaName()).append(
					"\u7684\u7B80\u5386\u6210\u529F\u3002").toString());
		applier = null;
		if ("filterResume".equals(srcAction))
			return "filterResume";
		if ("resumeManage".equals(srcAction))
			return "resumeManage";
		if ("recruitapplierSearch".equals(srcAction))
			return "recruitapplierSearch";
		else
			return "input";
	}

	public String checkApplierInfo(String applierName, String applierPhone,
			String applierEmail, HttpSession session) {
		if (session == null || WebContextFactory.get() == null
				|| WebContextFactory.get().getSession() == null)
			return null;
		String auth = DWRUtil.checkAuth("recruitapplierCreateInit", "execute");
		if (!"HR".equals(auth) && !"HRM".equals(auth))
			return "noauth";
		List appInfoList = null;
		if (!StringUtils.isEmpty(applierName)
				&& !StringUtils.isEmpty(applierPhone))
			appInfoList = getApplierByInfo(applierName, applierPhone);
		List appEmailList = null;
		if (!StringUtils.isEmpty(applierEmail))
			appEmailList = getApplierByInfo(applierEmail);
		StringBuilder message = new StringBuilder();
		message.append(geneBackMessage(appInfoList, applierName, applierPhone,
				null));
		message.append(geneBackMessage(appEmailList, null, null, applierEmail));
		return message.toString();
	}

	private String geneBackMessage(List appList, String applierName,
			String applierPhone, String applierEmail) {
		if (appList == null)
			return "";
		StringBuilder returnMessage = new StringBuilder();
		for (int j = 0; j < appList.size(); j++) {
			if (applierName != null
					&& ((Recruitapplier) appList.get(j)).getRecaName().equals(
							applierName)
					&& ((Recruitapplier) appList.get(j)).getRecaPhone().equals(
							applierPhone)) {
				if (returnMessage.length() > 0)
					returnMessage.append(";");
				returnMessage
						.append(
								(new StringBuilder()).append(applierName)
										.append("-").append(applierPhone)
										.toString())
						.append(
								"\uFF0C\u5728\u7CFB\u7EDF\u4E2D\u5DF2\u5B58\u5728\uFF01")
						.append(
								(new StringBuilder()).append(",").append(
										((Recruitapplier) appList.get(j))
												.getId()).toString());
			}
			if (applierEmail == null
					|| !((Recruitapplier) appList.get(j)).getRecaEmail()
							.equals(applierEmail))
				continue;
			if (returnMessage.length() > 0)
				returnMessage.append(";");
			returnMessage.append(applierEmail).append(
					"\uFF0C\u5728\u7CFB\u7EDF\u4E2D\u5DF2\u5B58\u5728\uFF01")
					.append(
							(new StringBuilder()).append(",").append(
									((Recruitapplier) appList.get(j)).getId())
									.toString());
		}

		if (returnMessage.length() > 0)
			return returnMessage.toString();
		else
			return "";
	}

	private List getApplierByInfo(String applierName, String applierPhone) {
		DetachedCriteria dc1 = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		BaseCrit.addDC(dc1, Recruitapplier.PROP_RECA_NAME, "eq",
				new String[] { applierName.trim() });
		BaseCrit.addDC(dc1, Recruitapplier.PROP_RECA_PHONE, "eq",
				new String[] { applierPhone.trim() });
		IRecruitapplierBo appBo = (IRecruitapplierBo) getBean("applierBo");
		List appList = appBo.loadApplierByDc(dc1, page);
		if (appList != null && appList.size() == 0)
			return null;
		else
			return appList;
	}

	private List getApplierByInfo(String applierEmail) {
		DetachedCriteria dc1 = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitapplier.class);
		BaseCrit.addDC(dc1, Recruitapplier.PROP_RECA_EMAIL, "eq",
				new String[] { applierEmail.trim() });
		IRecruitapplierBo appBo = (IRecruitapplierBo) getBean("applierBo");
		List appList = appBo.loadApplierByDc(dc1, page);
		if (appList != null && appList.size() == 0)
			return null;
		else
			return appList;
	}

	public String hasApplier(String applierName, String applierPhone,
			String applierEmail) {
		String auth = DWRUtil.checkAuth("recruitapplierCreateInit", "execute");
		if (!"HR".equals(auth) && !"HRM".equals(auth))
			return null;
		List appInfoList = null;
		if (!StringUtils.isEmpty(applierName)
				&& !StringUtils.isEmpty(applierPhone))
			appInfoList = getApplierByInfo(applierName, applierPhone);
		List appEmailList = null;
		if (!StringUtils.isEmpty(applierEmail))
			appEmailList = getApplierByInfo(applierEmail);
		StringBuilder message = new StringBuilder();
		message.append(geneBackMessage(appInfoList, applierName, applierPhone,
				null));
		message.append(geneBackMessage(appEmailList, null, null, applierEmail));
		return message.toString();
	}

	public Recruitapplier getApplier() {
		return applier;
	}

	public void setApplier(Recruitapplier applier) {
		this.applier = applier;
	}

	public void upload(File tempfile, String resumeType) throws Exception {
		if (tempfile == null)
			return;
		String type = "";
		String oldFile = "";
		if (CHINESE_RESUME.equalsIgnoreCase(resumeType)) {
			type = resume1FileName.substring(resume1FileName.lastIndexOf("."));
			oldFile = applier.getRecaResume1();
		} else {
			oldFile = applier.getRecaResume2();
			type = resume2FileName.substring(resume2FileName.lastIndexOf("."));
		}
		String newFileName = (new StringBuilder()).append(
				FileOperate.buildFileName()).append(type).toString();
		FileOperate.buildFile(tempfile, "sys.recruitment.applier.path",
				"sys.recruitment.applier.type",
				"sys.recruitment.applier.length", new String[] { newFileName,
						oldFile });
		if (CHINESE_RESUME.equalsIgnoreCase(resumeType))
			applier.setRecaResume1(newFileName);
		else
			applier.setRecaResume2(newFileName);
	}

	public File getResume1() {
		return resume1;
	}

	public void setResume1(File resume1) {
		this.resume1 = resume1;
	}

	public File getResume2() {
		return resume2;
	}

	public void setResume2(File resume2) {
		this.resume2 = resume2;
	}

	public List getRecruitchannelList() {
		return RecruitchannelList;
	}

	public void setRecruitchannelList(List recruitchannelList) {
		RecruitchannelList = recruitchannelList;
	}

	public List getRecruitplanList() {
		return RecruitplanList;
	}

	public void setRecruitplanList(List recruitplanList) {
		RecruitplanList = recruitplanList;
	}

	public List getApplierStatus() {
		return applierStatus;
	}

	public void setApplierStatus(List applierStatus) {
		this.applierStatus = applierStatus;
	}

	public Recruitplan getPlan() {
		return plan;
	}

	public void setPlan(Recruitplan plan) {
		this.plan = plan;
	}

	public String getPlanID() {
		return planID;
	}

	public void setPlanID(String planID) {
		this.planID = planID;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getResume1FileName() {
		return resume1FileName;
	}

	public void setResume1FileName(String resume1FileName) {
		this.resume1FileName = resume1FileName;
	}

	public String getResume2FileName() {
		return resume2FileName;
	}

	public void setResume2FileName(String resume2FileName) {
		this.resume2FileName = resume2FileName;
	}

	public String getSrcAction() {
		return srcAction;
	}

	public void setSrcAction(String srcAction) {
		this.srcAction = srcAction;
	}

	private List RecruitchannelList;
	private List RecruitplanList;
	private List applierStatus;
	private Recruitapplier applier;
	private Recruitplan plan;
	private List errors;
	File resume1;
	File resume2;
	private String resume1FileName;
	private String resume2FileName;
	private String planID;
	private Pager page;
	private String CHINESE_RESUME;
	private String ENGLISH_RESUME;
	private String srcAction;
}
